/*
 * panelAuditoria.java
 *
 * Created on 23 de agosto de 2008, 01:52 PM
 */

package administrador;

/**
 *
 * @author  Jorge
 */
public class panelAuditoria extends javax.swing.JPanel {

        /** Creates new form Auditoria */
    javax.swing.JTable tabla;
    javax.swing.JScrollPane js;
    Conexion conexion;
    Facturacion factPrin;
    int opcionComprovante=0; // esto de aqui es para verificar si vamos a ver factutas
                             // anuladas o activas 
    
    public panelAuditoria(Conexion conexion) {
        initComponents();     
        this.conexion=conexion;
        llenaCombox();
    }
   public String consultaParametro(String tabla,String Columna,String Valor){
        String consulta=consulta="select * from aud_"+tabla+" where "+Columna+" like '"+Valor+"%'";
        return consulta;
    }
    public void muestroTabla(java.sql.ResultSet respuesta){
     //Obtengo Filas-Arreglo de Titulos de Columnas
        int filas=conexion.obtenerFilas(respuesta);
        String[] titulosCol=conexion.obtenerColumnas(respuesta);
        //Creo la tabla:
        CrearTabla(filas,titulosCol);
        llenaTabla(respuesta,titulosCol.length,filas);
    }
    public void CrearTabla(int filas,String[] titulos){
        
        int columnas=titulos.length;
        String[][] tabla0=new String[filas][columnas];
       
        tabla= new javax.swing.JTable(tabla0,titulos);
        
        //Aqui le especifico para que la tabla acepte valores doubles e int:
        //--------------------------------------------------------------------
        final boolean[] canEdit = new boolean [columnas];
        for(int p=0;p<columnas;p++){
        canEdit[p]=false;
        }
        tabla.setModel(new javax.swing.table.DefaultTableModel(
            tabla0,titulos  
        ) {
           // boolean[] canEdit = new boolean [] { false, false, false, false};
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        //---------------------------------------------------------------------
                   
        //Le doy el evento del LCick del Mouse:
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
               // tablaMouseClicked(evt);
            }
        });
        
        //remuevo el JScroll para que no se monte cuando cree el nuevo:
        //*************************************************************
        try{
                panel_tabla.remove(js);
        }catch(java.lang.NullPointerException e){}
        //*************************************************************
        //Pongo en la tabla:
            js = new javax.swing.JScrollPane(tabla);
            js.setBounds(0,0,1000,500);
            js.setVisible(true);
            panel_tabla.add(js);
            js.updateUI();
    }
    public void llenaTabla(java.sql.ResultSet r,int columnas,int filas){
        try{
            r.beforeFirst();
            
            int j=0;
            while(r.next()) {
                
                for(int i=0;i<columnas;i++) {
                    
                    Object toma=r.getObject(i+1);
                    //System.out.println("toma valor:"+toma+" * fila:"+j+" * column:"+i);
                    try{
                        tabla.setValueAt(toma,j,i);
                    }catch(java.lang.ArrayStoreException e){
                        System.out.print("Errrrorrr");
                        tabla.setValueAt("null",j,i);
                    }
                }
                
                j++;
            }
        }catch(java.sql.SQLException sqle){
            System.out.println("Error al mostrar los datos");
            System.err.println(sqle);
        }
    }
    int indice=0;
    String tablaAditoria="";
    public void llenaCombox(){
        java.util.Vector listaPincipal=conexion.consulta("show tables where Tables_in_bdmultiadmin like 'aud_%'");
        java.util.Vector vectorInterno=new java.util.Vector();
        for(int i=0;i<listaPincipal.size();i++){
            vectorInterno=(java.util.Vector)listaPincipal.elementAt(i);
            combo_Tabla.addItem(vectorInterno.elementAt(0).toString().substring(4,vectorInterno.elementAt(0).toString().length()).toUpperCase());
        }
        tablaAditoria=combo_Tabla.getSelectedItem().toString();
        llenaComboColumnas(tablaAditoria);
    }
    public void llenaComboColumnas(String tablaAuditada){
        java.util.Vector listaPincipalCampo=conexion.consulta("SHOW COLUMNS FROM "+"aud_"+tablaAuditada);
        java.util.Vector vectorInternoCampo=new java.util.Vector();
        for(int i=0;i<listaPincipalCampo.size();i++){
            vectorInternoCampo=(java.util.Vector)listaPincipalCampo.elementAt(i);
            combo_Campo.addItem(vectorInternoCampo.elementAt(0).toString());
        }
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        combo_Tabla = new javax.swing.JComboBox();
        txt_Buscar = new javax.swing.JTextField();
        combo_Campo = new javax.swing.JComboBox();
        panel_tabla = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        combo_Tabla.setName("combo_Tabla"); // NOI18N
        combo_Tabla.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                combo_TablaPropertyChange(evt);
            }
        });

        txt_Buscar.setName("txt_Buscar"); // NOI18N
        txt_Buscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_BuscarKeyTyped(evt);
            }
        });

        combo_Campo.setName("combo_Campo"); // NOI18N

        panel_tabla.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panel_tabla.setName("panel_tabla"); // NOI18N

        javax.swing.GroupLayout panel_tablaLayout = new javax.swing.GroupLayout(panel_tabla);
        panel_tabla.setLayout(panel_tablaLayout);
        panel_tablaLayout.setHorizontalGroup(
            panel_tablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 939, Short.MAX_VALUE)
        );
        panel_tablaLayout.setVerticalGroup(
            panel_tablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 418, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txt_Buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(combo_Tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(combo_Campo, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panel_tabla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(combo_Campo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(combo_Tabla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txt_Buscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(panel_tabla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
private void combo_TablaPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_combo_TablaPropertyChange
    combo_Campo.removeAllItems();
    tablaAditoria=combo_Tabla.getSelectedItem().toString();
    llenaComboColumnas(tablaAditoria);
}//GEN-LAST:event_combo_TablaPropertyChange
int categoria=0;java.sql.ResultSet respuesta=null;
private void txt_BuscarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_BuscarKeyTyped
    try{
           try{
                panel_tabla.remove(js);
           }catch(java.lang.NullPointerException e){}
            muestroTabla(conexion.consultarBase(consultaParametro(combo_Tabla.getSelectedItem().toString(),combo_Campo.getSelectedItem().toString(),txt_Buscar.getText())));
            setVisible(true);
    }catch(Exception ex){}
}//GEN-LAST:event_txt_BuscarKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox combo_Campo;
    private javax.swing.JComboBox combo_Tabla;
    private javax.swing.JPanel panel_tabla;
    private javax.swing.JTextField txt_Buscar;
    // End of variables declaration//GEN-END:variables
}
